package com.sso.manager;

import com.sso.dao.SysMenuRoleMapper;
import com.sso.entity.SysMenuRole;
import com.sso.exception.DataBaseException;
import com.sso.exception.SelectException;
import com.tools.util.BaseUtils;
import com.tools.util.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysMenuRoleManager implements BaseManager<SysMenuRole> {
    @Autowired
    private SysMenuRoleMapper menuRoleDao;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(SysMenuRole entity) throws DataBaseException{
        int row=0;
        try {
            if (StrUtils.isEmpty(entity.getId())) {
                entity.setId(BaseUtils.getUUID());
                row = menuRoleDao.insert(entity);
            } else {
                row = menuRoleDao.update(entity);
            }
        }catch (Exception e){
            throw new DataBaseException(e);
        }
        return row;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<SysMenuRole> selectByPage(Map<String, Object> param) {
        return null;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<SysMenuRole> selectByProperty(Map<String, Object> param) {
        return null;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public long countByProperty(Map<String, Object> param) {
        return 0;
    }

    @Transactional(rollbackFor = Exception.class)
    public int deleteByRoleId(String roleId) throws Exception {
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("roleId",roleId);
        return menuRoleDao.delete(param);
    }

    @Transactional(rollbackFor = Exception.class)
    public int deleteByMenuId(String menuId) throws Exception {
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("menuId",menuId);
        return menuRoleDao.delete(param);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public List<SysMenuRole> selectByUserId(String userId){
        return menuRoleDao.selectByUserId(userId);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public List<SysMenuRole> selectByRoleId(String roleId){
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("roleId",roleId);
        return menuRoleDao.selectByProperty(param);
    }
}
